using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using T2.WebSecurity.ASPMVC.Entity.Interface;
using T2.WebSecurity.ASPMVC.Entity;
using T2.WebSecurity.Business.Entities;
using T2.WebSecurity.Business.Entities.Interfaces;
using T2.WebSecurity.ASPMVC.Entity.Search;
using T2.WebSecurity.ASPMVC.Entity.Search.Interface;
using T2.WebSecurity.Business.Entities.Search;
using T2.WebSecurity.Business.Entities.Search.Interfaces;
using T2.WebSecurity.ASPMVC.Entity.Translators;
using T2.WebSecurity.ASPMVC.Entity.Translators.Search;
using System.Activities;
using T2.WebSecurity.Business.Workflows;
using System.Web.Security;


namespace T2.WebSecurity.ASPMVC.ViewModel
{

	public class ApplicationUserViewModel : BaseViewModel
	{
		Business.BusinessService bs = new Business.BusinessService();
		public ApplicationUserViewModel(bool loadParent)
		{
		if (loadParent)
			{
				this.LoadUserProfileList();
				this.LoadApplicationList();
			}
		}
		
		private String m_header;
		public String Header
        {
            get
            {
                return "Application User";
            }
			
			set
            {
                m_header = value;
            }
        }
		
		private String m_title;
		public String Title
        {
            get
            {
                return "Application User";
            }
			
			set
            {
                m_title = value;
            }
        }
		
		private IApplicationUser_ui m_model;
		public IApplicationUser_ui Model
		{
			get
			{
				return m_model;
			}
			set
			{
				m_model = value;
			}
		}
		private List<IUserProfile_ui> m_userprofilelist;
		public List<IUserProfile_ui> UserProfile_List
		{
			get
			{
				return m_userprofilelist;
			}
			set
			{
				m_userprofilelist = value;
			}
		}

		private List<IApplication_ui> m_applicationlist;
		public List<IApplication_ui> Application_List
		{
			get
			{
				return m_applicationlist;
			}
			set
			{
				m_applicationlist = value;
			}
		}

		 private void LoadUserProfileList()
		{
			IUserProfile_biz param = new UserProfile_biz();
			param.Action = "GetAll";
			param.PageNumber = 1;
			param.PageSize = 20;
			param.SortColumn = "UserId";
			param.SortType = "desc";
			param.TotalRows = -1;
			List<IUserProfile_biz> retVal = bs.UserProfileController_Get(param, true, true);
			List<IUserProfile_ui> LoadRetVal = new List<IUserProfile_ui>();
			UserProfileUITranslator trans = new UserProfileUITranslator();
			foreach (IUserProfile_biz item in retVal)
			{
				IUserProfile_ui temp = new UserProfile_ui();
				temp = trans.TranslateFromBusinessLayer(item);
				LoadRetVal.Add(temp);
			}
			m_userprofilelist = LoadRetVal;
		}

		 private void LoadApplicationList()
		{
			IApplication_biz param = new Application_biz();
			param.Action = "GetAll";
			param.PageNumber = 1;
			param.PageSize = 20;
			param.SortColumn = "ApplicationID";
			param.SortType = "desc";
			param.TotalRows = -1;
			List<IApplication_biz> retVal = bs.ApplicationController_Get(param, true, true);
			List<IApplication_ui> LoadRetVal = new List<IApplication_ui>();
			ApplicationUITranslator trans = new ApplicationUITranslator();
			foreach (IApplication_biz item in retVal)
			{
				IApplication_ui temp = new Application_ui();
				temp = trans.TranslateFromBusinessLayer(item);
				LoadRetVal.Add(temp);
			}
			m_applicationlist = LoadRetVal;
		}


        
		
		public void LoadModel(IApplicationUser_ui param)
        {
            ApplicationUserUITranslator trans = new ApplicationUserUITranslator();
            IApplicationUser_biz bizParam = trans.TranslateToBusinessLayer(param);
            List<IApplicationUser_biz> tempRetVal = bs.ApplicationUserController_Get(bizParam, true, true);
            IApplicationUser_ui retVal = new ApplicationUser_ui();
            if (tempRetVal != null && tempRetVal.Count > 0)
            {
                retVal = trans.TranslateFromBusinessLayer(tempRetVal.First());
            }

            m_model = retVal;

        }

        public Int32 Set(IApplicationUser_ui param)
        {
            ApplicationUserUITranslator trans = new ApplicationUserUITranslator();
            IApplicationUser_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationUsersNonQuery(), wfInput);

            IDictionary<string, int> r = (IDictionary<string, int>)wfOutput["RetVal"];

            var ret = r.ToList().Select(s => new { Key = s.Key, Value = s.Value });
            Int32 retVal = ret.Where(z => z.Key == "Return_Value").Select(m => m.Value).FirstOrDefault();
			return retVal;
        }

       

        public List<IApplicationUser_ui> SearchList(IApplicationUserSearch_ui param)
        {
            ApplicationUserUISearchTranslator transSearch = new ApplicationUserUISearchTranslator();
            IApplicationUserSearch_biz bizParam = transSearch.TranslateToBusinessLayer(param);
            List<IApplicationUser_biz> retValBiz = bs.ApplicationUserController_Search(bizParam, true, true);
            List<IApplicationUser_ui> retVal = new List<IApplicationUser_ui>();
            ApplicationUserUITranslator trans = new ApplicationUserUITranslator();
            foreach (IApplicationUser_biz item in retValBiz)
            {
                IApplicationUser_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

        

        public List<IApplicationUser_ui> ModelList(IApplicationUser_ui param)
        {

            ApplicationUserUITranslator trans = new ApplicationUserUITranslator();
            IApplicationUser_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationUsersQuery(), wfInput);

            IDictionary<string, int> r = (IDictionary<string, int>)wfOutput["RetVal"];

            IApplicationUser_biz[] retValBiz = (IApplicationUser_biz[])wfOutput["RetValCollection"];//bs.ApplicationUserController_Get(bizParam, true, true);
            List<IApplicationUser_ui> retVal = new List<IApplicationUser_ui>();
            foreach (IApplicationUser_biz item in retValBiz)
            {
                IApplicationUser_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }
		
	}
}
